package com.xueyi.system.productionManager.controller;

import com.alibaba.fastjson2.JSONObject;
import com.xueyi.common.security.utils.SecurityUtils;
import com.xueyi.system.BaseMyMsg.domain.dto.BaseMyMsgDto;
import com.xueyi.system.BaseMyMsg.domain.query.BaseMyMsgQuery;
import com.xueyi.system.BaseMyMsg.service.IBaseMyMsgService;
import com.xueyi.system.api.dict.domain.dto.SysDictDataDto;
import com.xueyi.system.api.dict.domain.query.SysDictDataQuery;
import com.xueyi.system.api.organize.domain.dto.SysUserDto;
import com.xueyi.system.dict.service.ISysDictDataService;
import com.xueyi.system.productionManager.domain.query.BaseProductionManagerQuery;
import com.xueyi.system.productionManager.domain.dto.BaseProductionManagerDto;
import com.xueyi.system.productionManager.service.IBaseProductionManagerService;
import com.xueyi.common.web.entity.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.xueyi.common.security.annotation.RequiresPermissions;
import com.xueyi.common.core.web.validate.V_A;
import com.xueyi.common.core.web.validate.V_E;
import com.xueyi.common.core.web.result.AjaxResult;
import com.xueyi.common.log.annotation.Log;
import com.xueyi.common.log.enums.BusinessType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.io.Serializable;
import java.math.BigDecimal;
import java.security.Security;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 生产管理管理 业务处理
 *
 * @author zlf
 */
@RestController
@RequestMapping("/productionManager")
public class BaseProductionManagerController extends BaseController<BaseProductionManagerQuery, BaseProductionManagerDto, IBaseProductionManagerService> {


    @Autowired
    private IBaseProductionManagerService baseProductionManagerService;

    @Autowired
    private ISysDictDataService dictDataService;

    @Autowired
    private IBaseMyMsgService baseMyMsgService;

    /** 定义节点名称 */
    @Override
    protected String getNodeName() {
        return "生产管理" ;
    }

    /**
     * 查询生产管理列表
     */
    @Override
    @GetMapping("/list")
    @RequiresPermissions(Auth.BASE_PRODUCTION_MANAGER_LIST)
    public AjaxResult list(BaseProductionManagerQuery productionManager) {
        String userName = SecurityUtils.getUser().getUserName();
        System.out.println("kjdshkdfjh "+userName);
        BaseMyMsgQuery baseMyMsg = new BaseMyMsgQuery();
        baseMyMsg.setReceiveUser(userName);
        baseMyMsg.setReceiveStatus("2");
        List<BaseMyMsgDto> msgDtoList = baseMyMsgService.selectList(baseMyMsg);
        for (BaseMyMsgDto msgDto : msgDtoList) {
            msgDto.setReceiveStatus("1");
            baseMyMsgService.update(msgDto);
        }

        return super.list(productionManager);
    }

    /**
     * 查询生产管理详细
     */
    @Override
    @GetMapping(value = "/{id}")
    @RequiresPermissions(Auth.BASE_PRODUCTION_MANAGER_SINGLE)
    public AjaxResult getInfoExtra(@PathVariable Serializable id) {
        return super.getInfoExtra(id);
    }

    /**
     * 生产管理导出
     */
    @Override
    @PostMapping("/export")
    @RequiresPermissions(Auth.BASE_PRODUCTION_MANAGER_EXPORT)
    @Log(title = "生产管理管理", businessType = BusinessType.EXPORT)
    public void export(HttpServletResponse response, BaseProductionManagerQuery productionManager) {
        super.export(response, productionManager);
    }

    /**
     * 生产管理新增
     */
    @Override
    @PostMapping
    @RequiresPermissions(Auth.BASE_PRODUCTION_MANAGER_ADD)
    @Log(title = "生产管理管理", businessType = BusinessType.INSERT)
    public AjaxResult add(@Validated({V_A.class}) @RequestBody BaseProductionManagerDto productionManager) {
        return super.add(productionManager);
    }

    /**
     * 生产管理修改
     */
    @Override
    @PutMapping
    @RequiresPermissions(Auth.BASE_PRODUCTION_MANAGER_EDIT)
    @Log(title = "生产管理管理", businessType = BusinessType.UPDATE)
    public AjaxResult edit(@Validated({V_E.class}) @RequestBody BaseProductionManagerDto productionManager) {
        return super.edit(productionManager);
    }

    /**
     * 生产管理批量删除
     */
    @Override
    @DeleteMapping("/batch/{idList}")
    @RequiresPermissions(Auth.BASE_PRODUCTION_MANAGER_DEL)
    @Log(title = "生产管理管理", businessType = BusinessType.DELETE)
    public AjaxResult batchRemove(@PathVariable List<Long> idList) {
        return super.batchRemove(idList);
    }


    /**
     * 获取详情
     * @param productionId
     * @return
     */
    @GetMapping("/getProductionDetail")
    public AjaxResult getProductionDetail(String productionId) {
        BaseProductionManagerDto managerDto = baseProductionManagerService.selectById(productionId);
        SysDictDataQuery dataQuery = new SysDictDataQuery();
        dataQuery.setCode("base_assignment_style");
        dataQuery.setValue(managerDto.getAssignmentStyle());
        List<SysDictDataDto> dtoList = dictDataService.selectList(dataQuery);
        SysDictDataQuery dictDataQuery = new SysDictDataQuery();
        dictDataQuery.setCode("base_agriculture_status");
        dictDataQuery.setValue(managerDto.getAgricultureStatus());
        List<SysDictDataDto> dataDtoList = dictDataService.selectList(dictDataQuery);
        managerDto.setAssignmentStyle(dtoList.get(0).getLabel());
        managerDto.setAgricultureStatus(dataDtoList.get(0).getLabel());
        return AjaxResult.success(managerDto);
    }


    /**
     * 农事记录统计
     */

    @GetMapping("/getAgriculturalCount")
    public AjaxResult getAgriculturalCount(String month){


        SysDictDataQuery dictDataQuery = new SysDictDataQuery();
        dictDataQuery.setCode("base_agro_type");
        List<SysDictDataDto> dataDtoList = dictDataService.selectList(dictDataQuery);


        List<BaseProductionManagerDto> list = baseProductionManagerService.findMothByList(month);
        for (SysDictDataDto dataDto : dataDtoList) {
            dataDto.setSort(0);
            for (BaseProductionManagerDto managerDto : list) {

                if(dataDto.getValue().equals(managerDto.getAgroType())){
                   dataDto.setSort(dataDto.getSort() + 1);
                }
            }
        }

        return AjaxResult.success(dataDtoList);
    }

    /**
     * 获取生产管理选择框列表
     */
    @Override
    @GetMapping("/option")
    public AjaxResult option() {
        return super.option();
    }

    interface Auth {
        /** 系统 - 生产管理管理 - 列表 */
        String BASE_PRODUCTION_MANAGER_LIST = "system:productionManager:list" ;
        /** 系统 - 生产管理管理 - 详情 */
        String BASE_PRODUCTION_MANAGER_SINGLE = "system:productionManager:single" ;
        /** 系统 - 生产管理管理 - 新增 */
        String BASE_PRODUCTION_MANAGER_ADD = "system:productionManager:add" ;
        /** 系统 - 生产管理管理 - 修改 */
        String BASE_PRODUCTION_MANAGER_EDIT = "system:productionManager:edit" ;
        /** 系统 - 生产管理管理 - 删除 */
        String BASE_PRODUCTION_MANAGER_DEL = "system:productionManager:delete" ;
        /** 系统 - 生产管理管理 - 导出 */
        String BASE_PRODUCTION_MANAGER_EXPORT = "system:productionManager:export" ;
    }
}
